package com.runen.infinitemovement.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.tool.CommonUtil;

import java.util.ArrayList;
import java.util.List;

public class EasyRingView extends View {

    private Context mContext;
    private Paint mPaint;
    private int mDefaultColor;
    private float mRingWidth;
    private String name;
    private List<AngleAndColorBean> mAngleAndColorList;

    public EasyRingView(Context context) {
        this(context, null);
    }

    public EasyRingView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EasyRingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mRingWidth = CommonUtil.dp2px(mContext, 18);
        mPaint.setStrokeWidth(mRingWidth);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mDefaultColor = ContextCompat.getColor(mContext, R.color.color_FFFF4E5B);
        name = "健走";
        mAngleAndColorList = new ArrayList<>();
        mAngleAndColorList.add(new AngleAndColorBean(360, mDefaultColor, name));
    }

    public void setRingWidth(float width) {
        mRingWidth = width;
        if (mPaint != null) {
            mPaint.setStrokeWidth(width);
            invalidate();
        }
    }

    /**
     * @param name needAddGray if angle sum less than 360, add to 360
     */
    public void setAngleAndColorList(String name, @NonNull List<String[]> scaleAndColorList) {
        this.name = name;
        mAngleAndColorList.clear();
        if (scaleAndColorList.size() == 0) {
            mAngleAndColorList.add(new AngleAndColorBean(360, mDefaultColor, name));
        } else {
            float angleSum = 0;
            for (String[] scaleAndColor : scaleAndColorList) {
                float angle;
                try {
                    angle = Float.parseFloat(scaleAndColor[0]) * 360;
                } catch (NumberFormatException e) {
                    angle = 0;
                }
                int color;
                try {
                    color = Color.parseColor(scaleAndColor[1]);
                } catch (Exception e) {
                    color = mDefaultColor;
                }
                if (angle > 0) {
                    mAngleAndColorList.add(new AngleAndColorBean(angle, color, name));
                    angleSum += angle;
                }
            }
            if (mAngleAndColorList.size() == 0) {
                mAngleAndColorList.add(new AngleAndColorBean(360, mDefaultColor, name));
            } else if (angleSum > 360) {
                mAngleAndColorList.get(0).setAngle(mAngleAndColorList.get(0).getAngle() - (angleSum - 360));
            } else if (angleSum < 360) {
                mAngleAndColorList.get(mAngleAndColorList.size() - 1).setAngle(mAngleAndColorList.get(0).getAngle() + (360 - angleSum));
            }
        }
        invalidate();
    }

    @SuppressLint("NewApi")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mAngleAndColorList == null || mAngleAndColorList.size() == 0) {
            return;
        }
        float left = mRingWidth / 2 + 4;
        float top = mRingWidth / 2 + 4;
        float right = getMeasuredWidth() - mRingWidth / 2 - 4;
        float bottom = getMeasuredHeight() - mRingWidth / 2 - 4;


        if (mAngleAndColorList.size() > 1) {
            //first draw max angle arc
            AngleAndColorBean maxAngleBean = mAngleAndColorList.get(0);
            mPaint.setColor(maxAngleBean.getColor());
            canvas.drawArc(left, top, right, bottom, 90, maxAngleBean.getAngle(), false, mPaint);
            float startAngle = 90;
            for (int i = mAngleAndColorList.size() - 1; i > 0; i--) {
                AngleAndColorBean angleAndColorBean = mAngleAndColorList.get(i);
                mPaint.setColor(angleAndColorBean.getColor());
                if (mAngleAndColorList.get(i).name.equals(name)) {
                    canvas.drawArc(left, top, right, bottom, startAngle - angleAndColorBean.getAngle(),
                            angleAndColorBean.getAngle(), false, mPaint);
                } else {
                    canvas.drawArc(left, top, right, bottom, startAngle - angleAndColorBean.getAngle(),
                            angleAndColorBean.getAngle(), false, mPaint);
                }

                startAngle -= angleAndColorBean.getAngle();
            }
            //avoid the last scale has two corner
            mPaint.setColor(maxAngleBean.getColor());
            canvas.drawArc(left, top, right, bottom, 90 + maxAngleBean.getAngle() / 2, maxAngleBean.getAngle() / 2, false, mPaint);
        } else {
            mPaint.setColor(mAngleAndColorList.get(0).getColor());
            canvas.drawArc(left, top, right, bottom, 0, 360, false, mPaint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        float x = event.getX();
        float y = event.getY();

        invalidate();
        return super.onTouchEvent(event);

    }

    private class AngleAndColorBean {

        private float angle;
        private int color;
        private String name;

        AngleAndColorBean(float angle, int color, String name) {
            this.name = name;
            this.angle = angle;
            this.color = color;
        }

        float getAngle() {
            return angle;
        }

        void setAngle(float angle) {
            this.angle = angle;
        }

        public int getColor() {
            return color;
        }

        public void setColor(int color) {
            this.color = color;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}